import {
    findRowIndexInListFromAttrName,
    generateUUID,
    initListVueVirtualUUID,
    isInArray, isInvalid,
    isNullOrUndefined,
    mergeFromObject,  resetSksTableBefore
} from "./core";
import {renderTable,resetMainTableQueryParamsByTabsFilterValue} from "./sks-app-utils";
import {newSksTable} from "./sks-model-utils";

export const sksPageMixin = {
    data(){
        return{
            allLoading: false,

            mainTable: {
                //主键
                uniqueId: undefined,

                //主要
                mainSksApp: undefined,
                //未使用
                //应用名称,根据改名称查询列配置信息
                // sksAppName: undefined,
                //未使用
                //通过 appName 查找显示列表配置信息
                // sksAppNameToJumpEditSksAppConfig: true,

                //使用 tableColumnList 渲染
                tableColumnListEnable: false,
                //自动获取的列信息
                tableColumnList: undefined,

                //使用 queryParamsColumnList 渲染
                queryParamsColumnListEnable: false,
                //自动获取的搜索信息
                queryParamsColumnList: undefined,


                //表过滤标签页
                tabsFilterValue: undefined,
                /**
                 //默认排序方式
                 tabsSksOrderByDefaultSetting: [5],
                 tabsSksParamsDefaultSetting:{
                    processStatusNotIn: undefined,
                    processStatusIn: undefined,
                },
                 tabsFilterValue: '1',
                 tabsFilterOptionList:[
                 {
                        dictValue: 0,
                        dictLabel: '全部',
                        sksParams: {
                        },
                    },
                 {
                        dictValue: 1,
                        dictLabel: '待审核',
                        sksParams: {
                            processStatusNotIn:[2],
                        },
                        virtualOrderByFilterList: [6],
                    },
                 ],
                 */
                //表过滤标签页选项
                tabsFilterOptionList: undefined,

                //变更标签页前要设置的默认值
                tabsSksParamsDefaultSetting: {},
                //还未设计方案
                tabsSksOrderByDefaultSetting: undefined,


                dataType: 1,

                queryParams:{
                    sksParams:{},
                    sksParamsLabel:{},
                    // start: 0,
                    // limit: 100,
                    pageSize: 10,
                    pageNum: 1,
                },
                list:[],
                total: 0,
                loading: true,
                elementLoadingText: '拼命加载中',

                //当前存在单选项,true 禁用
                single : true,
                //当前存在多选项,true 禁用
                multiple : true,
                //点击的行
                currentRow: {},
                //勾选的行ID集合
                ids:[],
                // 勾选的行
                selection: [],

                showTable: true,

                showAllColumnButton: true,
                showAllColumn: false,

                showSearchButton: false,
                showSearch: true,

                //是否显示分页条
                showPagination: true,

                uuid: generateUUID(),
                //el-table 中的主键属性名,不设置拖动排序将不会刷新排序
                rowKey: 'vueVirtualUUID',
                //拖动排序
                dragSort: false,
                //拖动排序字段名
                dragSortPropName: 'sort',
                dragSortTableTmp: undefined,

                //默认展开所有行
                defaultExpandAll: false,

                //主页面才启用
                isMainPage: true,
            },

            //正在读取dictMap的数据
            dictDataLoading: true,
            //字典数据都放这里
            dictDataMap: new Map(),

            //跳转过来的参数缓存起来
            jumpParamsCache: {
                //true: 已启用过滤
                enableFlag: false,
                sksParams: {

                },
                //重置时要清除的属性
                clearSksParams: [],
            },
            /**
             * 设置 mainTable.queryParams 的默认参数,pageSize,pageNum,sksParams 属性将会忽略
             * 用于页面中不使用sksParam进行查询的情况下
             */
            defaultQueryParams: {},
        }
    },
    beforeCreate(){
        console.log("混入的beforeCreated");

    },
    created() {
        console.log("sksPage.created");
        console.log("sks-page.created")
        this.sksInit();
    },
    mounted: function () {
        console.log("sks-page.mounted")
        this.getMainList()
    },
    methods: {
        sksInit(){
            let querySksParams=undefined;
            // console.log("this.$route.query",JSON.stringify(this.$route.query));
            querySksParams=JSON.parse(JSON.stringify(this.$route.query)).sksParams
            // console.log("querySksParams",querySksParams);
            this.clearQueryParamByJumpParamsCache();
            // console.log("clearQueryParamByJumpParamsCache");
            if(!isInvalid(querySksParams)){
                let jumpSksParams = JSON.parse(querySksParams);
                // console.log("jumpSksParams",jumpSksParams);
                for (let key of Object.keys(jumpSksParams)) {
                    if (!isInArray(this.jumpParamsCache.clearSksParams, key)) {
                        this.jumpParamsCache.clearSksParams.push(key)
                    }
                }
                mergeFromObject(this.jumpParamsCache.sksParams,jumpSksParams)
                // console.log("this.jumpParamsCache",this.jumpParamsCache)
            }
            //

            let mainTable=mergeFromObject(newSksTable(),this.mainTable,this.initMainTableParam())
            if(isNullOrUndefined(mainTable.linkMap)){
                mainTable.linkMap={}
            }
            resetMainTableQueryParamsByTabsFilterValue(mainTable)
            if(mainTable.isMainPage){
                this.appendJumpParamsCacheToQueryParam(mainTable)
            }
            this.resetMainTableQueryParamsToDefaultQueryParams(mainTable)
            this.mainTable=mainTable
            this.getMainList()
            this.sksInitAfter()
        },
        /**
         * 重新设置 mainTableQueryParams 中的查询参数值为 defaultQueryParams 中的值
         */
        resetMainTableQueryParamsToDefaultQueryParams(mainTable=this.mainTable){
            for (const key in this.defaultQueryParams) {
                if(['pageSize','pageNum','sksParams'].indexOf(key)==-1){
                    mainTable.queryParams[key]= this.defaultQueryParams[key]
                }
            }
            console.log("resetMainTableQueryParamsToDefaultQueryParams.mainTable.queryParams",mainTable.queryParams)
        },
        clearQueryParamByJumpParamsCache(){
            let jumpSksParams = this.jumpParamsCache.sksParams
            for (let key of Object.keys(jumpSksParams)) {
                jumpSksParams[key]=undefined
            }
            this.jumpParamsCache.enableFlag=false
        },
        //拼接跳转参数到查询
        appendJumpParamsCacheToQueryParam(mainTable=this.mainTable){
            let jumpSksParams = this.jumpParamsCache.sksParams
            if(!isNullOrUndefined(jumpSksParams)){
                for (let key of Object.keys(jumpSksParams)) {
                    this.jumpParamsCache.enableFlag=true
                }
            }
            mergeFromObject(mainTable.queryParams.sksParams,jumpSksParams)
        },
        handleChangeTabsFilterValueChange(){
            // console.log("handleChangeTabsFilterValueChange")
            resetMainTableQueryParamsByTabsFilterValue(this.mainTable)
            // if(this.mainTable.tabsFilterOptionList&&!isNullOrUndefined(this.mainTable.tabsFilterValue)){
            //     let row = findRowInListFromAttrName(this.mainTable.tabsFilterOptionList,'dictValue',this.mainTable.tabsFilterValue)
            //     this.mergeFromObject(this.mainTable.queryParams.sksParams,this.mainTable.tabsSksParamsDefaultSetting,row?.sksParams)
            //     this.mainTable.queryParams.mybatisRestrictionName=row?.mybatisRestrictionName
            //     this.mainTable.queryParams.virtualOrderByFilterList=this.mainTable.tabsSksOrderByDefaultSetting
            //     console.log('handleChangeTabsFilterValueChange.row',row)
            //     if (row?.virtualOrderByFilterList) {
            //         this.mainTable.queryParams.virtualOrderByFilterList=row?.virtualOrderByFilterList
            //     }
            // }
            this.resetQuery()
        },
        sksInitAfter(){

        },
        /**
         * 初始化 mainTable 的一些值
         * @return {}  例如: {showSearchButton: true} 显示工具栏的搜索放大镜
         */
        initMainTableParam(){
            return {}
        },
        getMainListRemote(){
            return new Promise((resolve, reject) => {
                resolve({
                    data:{
                        list:[],
                        total: 0,
                    }
                })
            });
        },
        handleQuery(){
            this.mainTable.queryParams.pageNum=1
            this.getMainList()
        },
        resetQuery(){
            this.resetMainTableQueryParamsToDefaultQueryParams()
            this.resetQueryBefore()
            this.clearQueryParamByJumpParamsCache()
            // console.log("tabsFilterValueChange",this.tabsFilterValueChange)
            resetMainTableQueryParamsByTabsFilterValue(this.mainTable)
            this.handleQuery()
        },
        resetQueryBefore(){

        },
        getMainList(){
            // this.mainTable.list=[]
            // this.mainTable.currentRow=undefined
            // this.mainTable.loading=true
            resetSksTableBefore(this.mainTable)
            this.getMainListRemote(this.mainTable.queryParams).then(response=>{
                const res=this.handleSuccessList(response)
                this.mainTable.loading=false
                let list = res.data.list
                initListVueVirtualUUID(list)
                this.mainTable.list= list
                this.mainTable.total=res.data.total
           }).catch(error=>{
           })
        },
        //处理成功的数据
        handleSuccessList(res){
            return res
        },


        convertAqoeousList(mainPage){
          return {
              data: {
                  list: mainPage.list,
                  total: mainPage.total,
              }
          }
        },
        //更新list中的行,根据 uniqueId
        updateMainTableRowByUniqueId(row){
            let uniqueId = this.mainTable.uniqueId;
            let index = findRowIndexInListFromAttrName(this.mainTable.list,uniqueId,row[uniqueId]);
            if(index==-1){
               return
            }
            this.$set(this.mainTable.list,index,row);

        },

        renderTable(){
            //table 用于触发重新渲染table
            renderTable(this.mainTable)
        },
    },
}
